package jsonlib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import domain.Province;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *数组和集合都是用JSONArray.fromObject() 
 */
public class Demo {
	@Test     //这个的所用是注释，他是JUnite的测试基础，作用是指定抛出类，以及测试代码运行的时间
	//数组
	public void f1(){
		String[] arr={"aa","bb","cc"};
		JSONArray json=JSONArray.fromObject(arr);
		System.out.println(json);
		//结果是["aa","bb","cc"]
	}
	
	@Test
	//集合
	public void f2(){
		List<String> list=new ArrayList();
		list.add("11");
		list.add("22");
		list.add("33");
		JSONArray jsonlist=JSONArray.fromObject(list);
		System.out.println(jsonlist);
		//结果是["11","22","33"]
	}
	
	@Test
	//bean对象
	public void f3(){
		Province pro=new Province();
		pro.setName("北京");
		pro.setProvinceid(1);
		
		JSONObject jsonpro=JSONObject.fromObject(pro);
		
		System.out.println(jsonpro);
		//结果是{"name":"北京","provinceid":1}
	}
	
	@Test
	//map
	public void f4(){
		Map map=new HashMap();
		map.put("电话", "5105094");
		map.put("住址","广州");
		
		JSONObject jsonmap=JSONObject.fromObject(map);
		System.out.println(jsonmap);
		//结果是{"电话":"5105094","住址":"广州"}
	}
	
	@Test
	//provincebean
	public void f5(){
		List<Province>list=new ArrayList();
		list.add(new Province(1,"bj"));
		list.add(new Province(2,"tj"));
		list.add(new Province(3,"nj"));
		
		JSONArray jsonpro=JSONArray.fromObject(list);
		System.out.println(jsonpro);
		//结果是[{"name":"bj","provinceid":1},{"name":"tj","provinceid":2},{"name":"nj","provinceid":3}]
	}
}
